package com.test07_继承与多态;

import org.junit.Test;

/**
 * 多态性
 */
public class YootkDemo_05 {

    @Test
    public void test1() {
        Book5 book = new MathBook5();
        book.read();
        MathBook5 math = (MathBook5) book;
        math.calc();
    }

    @Test
    public void test2() {
        {   // 第一组判断操作
            Book5 book = new Book5();
            System.out.println("【1】book对象是否为Book类的实例：" + (book instanceof Book5));
            System.out.println("【1】book对象是否为MathBook类的实例：" + (book instanceof MathBook5));
        }
        System.out.println("====================== 两组判断的友善分割线 ======================");
        {   // 第二组的判断操作
            Book5 book = new MathBook5();
            System.out.println("【2】book对象是否为Book类的实例：" + (book instanceof Book5));
            System.out.println("【2】book对象是否为MathBook类的实例：" + (book instanceof MathBook5));
        }
    }

    @Test
    public void test3() {
        Book5 book = new MathBook5();   // 向上转型
        book.read();
        if (book instanceof MathBook5) {
            MathBook5 math = (MathBook5) book;  // 向下转型
            math.calc();
        }
    }

    @Test
    public void test4() {
        Book5 book = null;
        System.out.println(book instanceof Book5);
    }
}

class Book5 {
    public void read() {
        System.out.println("【Book】开始进行图书的阅读");
        System.out.println(this.getInfo());
    }

    public String getInfo() {
        return "【Book】这是一本书，创作者：www.yootk.com";
    }
}

class MathBook5 extends Book5 {

    @Override
    public String getInfo() {
        return "【MathBook】这是一本关于计算机数学类的图书";
    }

    public void calc() {
        System.out.println("【MathBook】根据数学书中讲解的公式进行数学计算");
        System.out.println(this.getInfo());
    }
}
